package com.cmc6.minio.controller;

import com.cmc6.common.util.ApiResult;
import com.cmc6.minio.model.dto.FileDownloadDTO;
import com.cmc6.minio.model.dto.FileRemoveDTO;
import com.cmc6.minio.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/file")
@Api(tags = "文件控制器")
public class FileController {

    @Resource
    FileService baseService;

    @ApiOperation(value = "文件上传")
    @PostMapping("/upload")
    public ApiResult<String> upload(@RequestParam MultipartFile file, @RequestParam String bucketName,
        @RequestParam String folderName, @RequestParam(required = false) String remark,
        @RequestParam(required = false) String extraJson) {
        return ApiResult.ok("上传成功", baseService.upload(file, bucketName, folderName, remark, extraJson));
    }

    @ApiOperation(value = "公用，文件下载（不需要登录）")
    @GetMapping("/publicDownload")
    public void publicDownload(@RequestParam String uri) {
        baseService.publicDownload(uri);
    }

    @ApiOperation(value = "文件下载")
    @PostMapping("/download")
    public void download(@RequestBody @Valid FileDownloadDTO dto) {
        baseService.download(dto);
    }

    @ApiOperation(value = "文件删除")
    @PostMapping("/remove")
    public ApiResult<String> remove(@RequestBody @Valid FileRemoveDTO dto) {
        return ApiResult.ok(baseService.remove(dto));
    }

}
